﻿/// Copyright © 2013 Microsoft Corporation. 
/// This project is released under the Microsoft Public License (MS-PL)
///
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using Azure.Performance.Storage;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.Storage;

namespace Azure.Performance.ConsoleHost
{
    class Program
    {
        static void Main(string[] args)
        {
            ServicePointManager.UseNagleAlgorithm = false;
            ServicePointManager.Expect100Continue = false;
            ServicePointManager.DefaultConnectionLimit = 120;

            IUploadPerformanceTest[] tests = {
                new UploadPerformanceTest(),
                new UploadPerformanceTestSdk20WritePages(),
            };

            int[] threads = { 2, 4, 8, 16, 32, 64 };
            //int[] threads = { 2, 4, 8, 16 };

            var p = new Program();

            for (var i = 0; i < 50; i++)
            {
                foreach (var test in tests)
                {
                    foreach (var thread in threads)
                    {
                        p.RunTest(test, 256, false, thread);
                        p.RunTest(test, 256, true, thread);
                    }
                }
            }
        }

        private void RunTest(IUploadPerformanceTest test, int fileSize, bool useSsl, int threads)
        {
            Thread.Sleep(2000);

            var file1 = TestFile.GetFile(fileSize);

            var connectionString = CloudConfigurationManager.GetSetting("Application.Storage");

            //TODO: pass this in and let the performance tests set it
            if (useSsl)
                connectionString = connectionString.Replace(
                    "DefaultEndpointsProtocol=http;", "DefaultEndpointsProtocol=https;");
            else
                connectionString = connectionString.Replace(
                    "DefaultEndpointsProtocol=https;", "DefaultEndpointsProtocol=http;");

            test.Threads = threads;
            test.Initialize(connectionString);

            var stopwatch = new Stopwatch();

            var start = DateTime.UtcNow;
            stopwatch.Start();
            test.Upload(file1);
            stopwatch.Stop();
            var end = DateTime.UtcNow;

            var result = new TestResultEntity()
            {
                TestId = test.Id,
                TestName = test.Name,
                StartTime = start,
                Threads = threads,
                Protocol = useSsl ? "https" : "http",
                CompletedTime = end,
                FileSize = string.Format("{0}mb", fileSize),
                StorageAccount = "---YOURACCOUNT---",
                UploadTime = string.Format("{0:N2}", stopwatch.Elapsed.TotalSeconds),
                AverageThroughput = string.Format("{0:N2}", fileSize / stopwatch.Elapsed.TotalSeconds),
                DeploymentId = String.Empty,
                Location = "On Premises",  //TODO: Move this to configuration or discover this
                InstanceSize = BenchmarkHelper.GetInstanceSize()
            };

            var account = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("Application.Storage"));

            var repository = new TestResultRepository(account);
            repository.Add(result);

            test.Clean();
        }
    }
}
